package com.freshcity.page.feedback
{
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.component.container.VBox;
	import com.freshcity.core.BasePage;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.graphics.FeedBackPage1;
	import com.freshcity.graphics.FeedBackPage2;
	import com.freshcity.graphics.FeedBackWin;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.page.wait.RoomList;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Strong;
	import com.pblabs.core.PBGameObject;
	
	import fl.controls.CheckBox;
	import fl.controls.Label;
	import fl.data.DataProvider;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	public class FeedBack extends BaseGameObject
	{
		public var win:FeedBackWin;
		private var submitObject:Object={};
		private var toUser:Object;
		private var page1:FeedBackPage1;
		private var page2:FeedBackPage2;
		private var propertyBox:VBox;
		private var mine:*;
		
		override public function initialize():void
		{
			super.initialize();
			
			win=new FeedBackWin();
			win.visible = false;
			LayerManager.sceneLayer.centerPopup(win);
			win.y -= 600;
			parent.addChild(win);
			
			var mask:Shape = new Shape();
			mask.graphics.beginFill(0xffffff);
			mask.graphics.drawRect(20,10,340,450);			
			
			page1 = new FeedBackPage1();
			win.addChild(page1);
			
			page2 = new FeedBackPage2();
			page2.mask = mask;
			win.addChild(mask);
			
			page1.picContent.addEventListener(FocusEvent.FOCUS_IN,onPicContentFocus);
			page1.picContent.addEventListener(Event.CHANGE,onPicContentChange);
			page1.submitBtn.addEventListener(MouseEvent.CLICK,onSubmitBtn1Click);
			page1.cancelBtn.addEventListener(MouseEvent.CLICK,onCloseBtn1Click);
			page2.submitBtn.addEventListener(MouseEvent.CLICK,onSubmitBtn2Click);
			page2.cancelBtn.addEventListener(MouseEvent.CLICK,onCloseBtn2Click);
		}
		
		protected function onPicContentChange(event:Event):void
		{
			var i:int = page1.picContent.maxChars;
			var j:int = StringUtil.getANSILength(page1.picContent.text);
			page1.limitWordAmt.text = String(i-j);
			if(j>0 && i-j >= 0){
				page1.submitBtn.enabled = true;
			}else{
				page1.submitBtn.enabled = false;
			}
		}
		
		protected function onPicContentFocus(event:FocusEvent):void
		{
			page1.limitWordAmt.text = String(page1.picContent.maxChars);
			page1.picContent.text = "";
		}
		
		private function switchNextPage(data:*=null):void
		{
			if(group.user.level<=7){
				group.commu.send(OpCodes.ROOM_LIST, {sid:this.group.user.stop});
				return;
			}
			
			var myfun:Function = function():void{
				page2.x = win.width;
				win.addChild(page2);
			};
			
			TweenLite.to(page1,0.1,{alpha:0});
			TweenLite.to(page2,0.5,{onInit:myfun,x:0,delay:0.1});
		}
		
		public function show(data:*):void
		{
			group.commu.register(OpCodes.ROOM_LIST,onLoad);
			this.toUser = data;
			
			page1.limitWordAmt.text = String(page1.picContent.maxChars);
			page1.picContent.text = "说说对TA的印象，也给自己提个醒。描述尽量具体详细，禁止空洞、毁谤、吹捧等不实言词。";
			page1.submitBtn.enabled = false;
			
			//属性评估
			var tip:Label = new Label();
			tip.width = 200;
			tip.text = "请选择可能不真实的信息：";
			propertyBox = new VBox();
			initlizeProperty(propertyBox);
			propertyBox.initialize();
			
			var g2:VBox = new VBox();
			g2.addChild(tip);
			g2.addChild(propertyBox);
			g2.initialize();
			page2.propertyPanel.source = g2;			
		}
		
		private function initlizeProperty(parent:VBox):void
		{
			this.mine = MainScene(this.owningGroup).user;
			
			for (var i:int = 0; i < mine.level; i++) 
			{
				var s1:String = GlobalVar.nameProperty[i];
				var s2:String = GlobalVar.labelProperty[i];
				createItem(s2,s1);
			}			
		}
		
		private function createItem(label:String,name:String):void
		{
			if(!toUser[name])return;
			var check:CheckBox = new CheckBox();
			check.name = name;
			check.label = label + ":" + toUser[name];
			check.setSize(200,30);
			check.addEventListener(MouseEvent.CLICK,onSelectProper);
			propertyBox.addChild(check);
		}		
		
		protected function onSelectProper(e:MouseEvent):void
		{
			var cb:CheckBox = CheckBox(e.target);
			if(cb.selected == true) {
				submitObject[cb.name] = "true";
			}
		}
		
		protected function onCloseBtn1Click(event:MouseEvent):void
		{
			switchNextPage();
		}
		
		protected function onSubmitBtn1Click(event:MouseEvent):void
		{
			var c:String = StringUtil.trim(page1.picContent.text);
			if(c.length<8){
				group.msg.showRollTipsMsg("描述过于简单啦");
				return;
			}
			group.commu.send(OpCodes.USER_COMMENT, {content:c},switchNextPage);		
		}		
		
		protected function onCloseBtn2Click(event:MouseEvent):void
		{
			group.commu.send(OpCodes.ROOM_LIST, {sid:this.group.user.stop});	
		}
		
		protected function onSubmitBtn2Click(event:MouseEvent):void
		{
			group.commu.send(OpCodes.USER_REPORT, submitObject);		
		}
		
		private function onLoad(data:Object):void
		{		
			group.commu.unRegister(OpCodes.ROOM_LIST);
			var rooms:RoomList = this.group.roomList;
			rooms.show(data);
			
			var myfun2:Function=function():void
			{
				if(win.contains(page2)){
					win.removeChild(page2);
				}
				
				page1.alpha = 1;
				
				LayerManager.sceneLayer.centerPopup(rooms.win);
				rooms.win.x += 800;
				
				LayerManager.sceneLayer.centerPopup(win);
				//this.win.y += 800;				
			};
			
			TweenMax.to(this.win, 0.3, {onInit:myfun2,autoAlpha:0, y: "-600"});
			TweenMax.to(rooms.win, 0.3, {x: "-800",autoAlpha:1, delay: 0.3});			
		}
		
	}
}